package com.thon.controller.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebParam.Mode;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresGuest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.dao.util.Page;
import com.thon.entity.user.Message;
import com.thon.entity.user.User;
import com.thon.service.user.MessageService;
import com.thon.service.user.UserService;


@Controller
@RequestMapping(value = "/api/message")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MessageRestController {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private UserService userService;
	
	//根据Message id获取message 并更新readTime
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> getMessage(@RequestParam(value = "id",required = true)int id){
		
		Message message = messageService.getMessage(id);
		
		message.setReadTime(new Date());
		messageService.updateMessage(message);
		
		return new ResponseEntity(message,HttpStatus.OK);
	}
	
	//添加站内通知
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ResponseEntity<HttpEntity> addMessage(@RequestBody Message message){
		User from=userService.getUser(message.getMessageFrom().getId());
		User to=userService.getUser(message.getMessageTo().getId());
		String content=message.getContent();
		messageService.sendMessage(from, to, content);
		return new ResponseEntity(HttpStatus.OK);
	}
	
	// 根据用户id获取通知
	@RequestMapping(value = "/list")
	public ResponseEntity<HttpEntity> findMessage(@RequestParam(value = "uid", required = true) int userId,
			@RequestParam(value = "p", defaultValue = "1") int pageNo,
			@RequestParam(value = "s", defaultValue = "10") int pageSize, Model model) {
		
		Page<Message> page = messageService.pageMessageTo(userId, pageNo, pageSize);
		if(page.getResult()!=null){
			Date readTime=new Date();
			for(Message m:page.getResult()){
				m.setReadTime(readTime);
				messageService.updateMessage(m);
			}
		}
		return new ResponseEntity(page, HttpStatus.OK);
	}
	
	//根据站内信id删除通知
	@RequestMapping(value = "/del",method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> deleteMessage(@RequestParam(value = "id") int messageId,Mode modle){
		
		messageService.deleteMessage(messageId);
		return new ResponseEntity(true,HttpStatus.NO_CONTENT);
	}
	
	

}
